#pragma once
#include <iostream>
#include <mysql_driver.h>
#include <mysql_connection.h>
#include <cppconn/prepared_statement.h>
#include <cppconn/statement.h>
#include <cppconn/resultset.h>
#include <cppconn/exception.h>
#include <chrono>
#include <ctime>
#include <iomanip>
#include <sstream>

class Data_get
{
public:
    Data_get(const std::string &host = "127.0.0.1:3306", const std::string &user = "root", const std::string &passwd = "123456")
    {
        _host = host;
        _user = user;
        _passwd = passwd;
    }

    void InitSql(const std::string &db = "conn")
    {
        _driver = sql::mysql::get_mysql_driver_instance();
        // 建立连接
        _con = _driver->connect(_host.c_str(), _user.c_str(), _passwd.c_str());
        // 选择数据库
        _con->setSchema(db.c_str());
        // 创建语句对象
        _stmt = _con->createStatement();
        std::cout << "数据库连接成功" << std::endl;
    }

    std::string getCurrentDateTime()
    {
        // 获取当前时间点
        auto now = std::chrono::system_clock::now();
        // 转换为time_t类型，以便使用ctime函数
        std::time_t now_time_t = std::chrono::system_clock::to_time_t(now);
        // 转换为本地时间
        std::tm *now_tm = std::localtime(&now_time_t);

        // 使用std::ostringstream来构建格式化的日期时间字符串
        std::ostringstream oss;
        oss << std::put_time(now_tm, "%Y-%m-%d %H:%M:%S");

        return oss.str();
    }

    void Savedata(const int &humi = 0, const int &temp = 0)
    {

        std::unique_ptr<sql::PreparedStatement> pstmt(_con->prepareStatement("INSERT INTO zib (date, humi,temp) VALUES (?, ?,?)"));
        pstmt->setString(1, getCurrentDateTime().c_str());
        pstmt->setInt(2, humi);
        pstmt->setInt(3, temp);

        int rows_affected = pstmt->executeUpdate();
        if (!rows_affected)
        {
            std::cout << "update databases error" << std::endl;
        }
        else
        {
            std::cout << "update databases: humi:" << humi<<" temp:" << temp << std::endl;
        }
    }

    void getdate(std::vector<std::string>& date,std::vector<int>& humi,std::vector<int>& temp)
    {
        sql::ResultSet *_res = _stmt->executeQuery("SELECT * from zib ");
        while (_res->next())
        {
            date.push_back(_res->getString("date"));
            humi.push_back(_res->getInt("humi"));
            temp.push_back(_res->getInt("temp"));
        }
        delete _res;
    }

    // void test()
    // {
    //     InitSql();
    //     // 处理结果集
    //     // 插入
    //     std::unique_ptr<sql::PreparedStatement> pstmt(_con->prepareStatement("INSERT INTO zib (date, humi,temp) VALUES (?, ?,?)"));
    //     pstmt->setString(1, getCurrentDateTime().c_str());
    //     pstmt->setString(2, "77");
    //     pstmt->setString(3, "30");

    //     int rows_affected = pstmt->executeUpdate();
    //     std::cout << "Rows affected: " << rows_affected << std::endl;

    //     // 查询
    //     sql::ResultSet *_res = _stmt->executeQuery("SELECT * from zib where id >6");
    //     while (_res->next())
    //     {
    //         std::cout << "\t... MySQL replies: " << _res->getString("date") << std::endl;
    //     }
    //     delete _res;
    // }

    ~Data_get()
    {
        // 清理
        delete _stmt;
        delete _con;
    }

private:
    std::string _host;
    std::string _user;
    std::string _passwd;
    sql::mysql::MySQL_Driver *_driver;
    sql::Connection *_con;
    sql::Statement *_stmt;
};
